# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=msmpi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.1.1
pkgrel=15
pkgdesc="Microsoft MPI SDK (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc")
makedepends=("${MINGW_PACKAGE_PREFIX}-fc")
optdepends=("${MINGW_PACKAGE_PREFIX}-fc: Build Fortran code")
checkdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
source=(
  'mpi.c'
  'mpi.h'
  'mpif.h'
  'mpi.f90'
  'mpifptr.h.x86_64'
  'mpifptr.h.i686'
  'msmpi.def.x86_64'
  'msmpi.def.i686'
  'hello_mpi.c'
  'hello_mpi.cpp'
  'hello_mpi.F'
  'hello_mpi.f90'
  'all.tcl'
  'tclbuildtest.tcl'
  'msmpi.test'
)
sha256sums=('17086fb1cf949251e4ae1549a06d292c58e468c822cca3ac1851e57e79d8ab20'
            'baee3f18f38650e7182956baa0d3d8f8e5c26d8603ccee4871a4dbd160c13660'
            '66185a4d47ca1a27df2340d3d8d28787e72db54b184f20430ed1e1d7c9244371'
            '5fbdb4109bb5d0badf24c4aaeeaa98009d4a7eaf4e595ecfa4bc3e750176299f'
            'edd76b5096bd31052be13e2d3306d931277d9c983b147ecf8aef0761d4ae8fb3'
            '126bb8230844ad1de5f7e368c12008d2eea9831cb149b4a6952c775c08b294a2'
            'fd18184993872fc4eaea825e85f974dca4b020598d838c424c6c112c2328cf2a'
            '7b8ed9d3e257e439678cc648164164d6817f7fde68530055b392f67d97bf8ec8'
            'ea8203492942fa900c4946741ad88af8957c4d058e06eb618ffae0e5e967b71b'
            '9483053c0f3ce15d850f69565f2b4e53630cee75073c27432cd08ecc596a57d9'
            'c9f63c1ca20f8ebfe26ad455342f34c27bc1a1a83778240388e9f18b26c2e889'
            '8cb47bb77451dbe2703f7f28eef422e55e7a33a28977bf13bbd11d305aeef61e'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '773d406e05340b3b96326ddd04f43fea3df3ca30912412824a976a18a86fc007')

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # Import library (.dll.a)
  mkdir -p lib
  ${MINGW_PREFIX}/bin/dlltool -k -d "$srcdir/msmpi.def.${CARCH}" -l lib/libmsmpi.dll.a

  # Headers
  mkdir -p include
  cp $srcdir/mpi.h include
  cp $srcdir/{mpif.h,mpi.f90} include
  cp $srcdir/mpifptr.h.${CARCH} include/mpifptr.h

  # C/C++ Compiler wrappers
  mkdir -p bin
  mpic="$srcdir/mpi.c"
  if [[ ${MSYSTEM} == CLANG64 ]]; then
    sed -e "s|gfortran|flang|g" -i ${mpic}
  fi
  cflags="-s -O2 -DNDEBUG"
  $CC ${cflags} -o bin/mpicc.exe -DCC "${mpic}"
  $CC ${cflags} -o bin/mpicxx.exe -DCXX "${mpic}"
  cp bin/mpicxx.exe bin/mpic++.exe

  # Fortran Compiler wrappers
  $CC ${cflags} -o bin/mpifort.exe -DFC "${mpic}"
  cp bin/mpifort.exe bin/mpif77.exe
  cp bin/mpifort.exe bin/mpif90.exe

  # FORTRAN90 MPI modules
  bin/mpifort -c -Jinclude -cpp "${srcdir}"/mpi.f90
  rm mpi.o
}

package() {
  cd "${srcdir}"

  cp -r "build-${MSYSTEM}" "${pkgdir}${MINGW_PREFIX}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"
  cp {all,tclbuildtest}.tcl hello_mpi.{c,cpp,F,f90} msmpi.test "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo "
    prefix=${MINGW_PREFIX}
    libdir=\${prefix}/lib
    includedir=\${prefix}/include
    Name: ${_realname}
    URL: ${url}
    Version: ${pkgver}
    Description: ${pkgdesc}
    Cflags: -I\${includedir}
    Libs: -L\${libdir} -l:libmsmpi.dll.a
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"

}
